// import { StyleService } from './../../providers/StyleService';
// import { BasePage } from './../basePage';
// import { Component, Injector } from '@angular/core';
// import { IonicPage} from 'ionic-angular';
// declare var $: any;
// /**
//  * Generated class for the AddLabelPage page.
//  *
//  * See https://ionicframework.com/docs/components/#navigation for more info on
//  * Ionic pages and navigation.
//  */

// @IonicPage()
// @Component({
//   selector: 'page-add-label',
//   templateUrl: 'add-label.html',
//   providers: [StyleService]
// })
// export class AddLabelPage extends BasePage  {
//    hands = [];
//   types= [];
//   tools = []
//   data = {};
//   constructor(injector: Injector,public styleProvider: StyleService) {
//     super(injector);
//   }

//   ionViewDidLoad() {
//     console.log('ionViewDidLoad AddLabelPage');
//     this.getSearchConditionAPI();
//   }
//   style(id) {
//     console.log(id)
//     $('.btns1 > div').eq(id).toggleClass('active')
     
//   }
//   style1(id) {
//     console.log(id)
//     $('.btns2 > div').eq(id).toggleClass('active1')
     
//   }
//     diss(){
//   console.log(111)
//     let data = { 'foo': 'bar' };
//     this.viewCtrl.dismiss(data);
//   }
//     /** 获取款式风格 */
//   async getSearchConditionAPI() {
//     let res = await this.styleProvider.getSearchConditionAPI(this.data);
//     console.log(res);
//     this.hands =res.hands;
//     this.types =res.type;
//     this.tools =res.tools;
//     console.log(this.hands);
//     console.log(this.types);
//     console.log(this.tools);
//   }
// }






import { Component, Injector, ElementRef, ViewChild } from '@angular/core';
import { Tabs } from 'ionic-angular';
import { IonicPage } from 'ionic-angular';
import { StorageService } from "./../../providers/StorageService";
import { UserPosVo } from "./../../model/UserPosVo";
import { BasePage } from "../basePage";
import { Events } from 'ionic-angular';
import { ContactPage } from "./../contact/contact";
import { RcmdPage } from "./../rcmd/rcmd";
import { LocalNotifications } from '@ionic-native/local-notifications';

import { NailShopPage } from "./../nail-shop/nail-shop";
import { StyleService } from './../../providers/StyleService';
import { VEERSION, PIC_URL, DOMAIN } from './../../providers//Config';
import { NativeService } from "../../providers/NativeService";
declare var layer;
declare let baidumap_location: any;
declare var $: any
@IonicPage()
@Component({
  selector: 'page-add-label',
  templateUrl: 'add-label.html',
  providers: [StyleService]
})
export class AddLabelPage extends BasePage {
  // styleData=[];
  hands = [];
  types= [];
  tools = []
  data = {};
  adList = [];
  popAdObj = [];
  picUrl = PIC_URL;
  currCity = "北京";
  userPosVo = {};
  contentArea = "RcmdPage";  //NailShopPage  美店
  //剩下弹出的款式 ,从 RcmdPage 赋值过来的
  otherStyleList = [];
  func = "recommend";
  constructor(injector: Injector, private localNotifications: LocalNotifications, public nativeService: NativeService,public stgSc: StorageService, public events: Events, public styleProvider: StyleService, ) {
    super(injector);

    // this.platform.ready().then((readySource) => {
    //   this.localNotifications.on('click', (notification, state) => {
    //     let eventData = JSON.parse(notification.data);
    //     alert("点击了本地通知")

    //   });
    // });

  }

  //每次调用
  ionViewDidEnter() {
    $('.btn').removeClass('active');
    $('.btn').eq(0).addClass('active');
    this.getSearchConditionAPI();
  }

  //只调用一次 ，注意：子的 ionViewDidLoad 在它之前
  ionViewDidLoad() {
    // document.title = "精选"
    // let stxt = document.querySelector(".searchbar-input");
    // stxt["disabled"] = true;

    //从缓存中取上次定位的城市 ，有，直接返回 -没有就百度定位，百度定位失败，让用户手工选城市
    //this.userPosVo = this.storageService.read('userMapInfo');
    if (this.userPosVo) {
      //this.currCity = this.userPosVo['city'];
      //this.doGetAd();
    }
    else {
      //this.getUserCurrentPos();
    }
    //先检查是否有升级 -----> 无 再弹出广告  ---------- 因为浏览器下无法取版本号，所以直接改为  config.js 来设置当前版本号了
    // this.platform.ready().then(async () => {
    //   let needUpdate = await this.nativeService.detectionUpgrade();
    //   if (!needUpdate) {  //不需要升级 
    //     this.doGetAd();
    //   }});
    //this.getNotifi();
  }

  /**
   * 去取广告数据
   */
  async doGetAd() {
    //取广告
    this.styleProvider.getAdlist({ pos_id: 3 }).then(
      (res) => {
        if (res) {
          this.navCtrl['_children'][0]._views[0].instance.adList = res.data;//轮播广告 
          //  this.popAdObj = res.data[2][0]; //弹出广告页
        }
      });

    //再读取本地版本与服务器版本
    // var res = await this.http.get(DOMAIN + "update.json?rnd=" + new Date().getTime()).toPromise()
    // var verObj = JSON.parse(res['_body']);
    // if (parseFloat(verObj['version']) > parseFloat(VEERSION)) {
    //   this.nativeService.promatUpdate(verObj['desc']);
    // } else {
    //   this.showPopAd()
    // }


  }

  getNotifi() {
    this.localNotifications.schedule({
      id: 1,
      title: '美甲艺境',
      text: '精选款式又有新品看，快去看看哪款适合你',
      data: {
        type: 0,  //0---- 打开网址
        url: ''
      }
    });
  }


  //打开右侧菜单
  openMenu() {
    this.menuCtrl.open();
  }


  //选择了首页某个大模块
  selectFunc(funcName) {
    this.contentArea = funcName;  //NailShopPage  美店
  }

  //得到用户当前坐标
  getUserCurrentPos = () => {
    this.platform.ready().then((readySource) => {
      try {
        baidumap_location.getCurrentPosition((result) => {
          // alert(JSON.stringify(result))
          if (!result.hasOwnProperty('city'))  //无网络无法定位,但百度还是根据浏览器返回了值
          {
            alert("没citi" + JSON.stringify(result));
            this.openCitiChoose();
            return;
          }

          this.userPosVo = result;
          this.currCity = this.userPosVo['city'];
          // alert('result' + JSON.stringify(result));
          //  alert('getUserCurrentPos' + JSON.stringify(this.userPosVo));
          this.stgSc.write('userMapInfo', this.userPosVo);
          //   alert("写入成功"+JSON.stringify(this.userPosVo))
        }, (error) => {

          // alert('百度定位信息读取失败');
          this.openCitiChoose();
          console.log("百度定位信息读取失败");
        });

      } catch (error) {
        // alert('百度定位组件初始化失败' + error);
        this.openCitiChoose();

        console.log("百度定位组件初始化失败");
      }
    });
  }



  /** 打开城市选择页面 */
  openCitiChoose = () => {
    this.navCtrl.push("CitySelectPage", {
      callback: this.getCitiChooseResult
    });
  }

  //得到城市选择的结果
  getCitiChooseResult = (data) => {
    return new Promise((resolve, reject) => {
      this.userPosVo = {};
      this.userPosVo['latitude'] = data.latitude;  //纬度
      this.userPosVo['longitude'] = data.longitude;
      this.userPosVo['city'] = data.name;
      this.currCity = this.userPosVo['city'];
      this.stgSc.write('userMapInfo', this.userPosVo);
      // this.doGetAd();
      resolve();
    });
    // 被调用者 this.callback(city).then(()=>{ this.navCtrl.pop() });
  }
  //身份切换
  // changeTabs($event) {
  //   this.events.publish("tab:change", 1)
  // }


  //选中了某个款式
  showMoreStyle(item) {
    this.navCtrl['_children'][0]._views[0].instance.showMoreStyle(item);
    // this.previousItem.innerText = item.type_name;
  }


  //显示大图广告
  showPopAd = () => {
    let modal = this.modalCtrl.create("AdPage");
    modal.present();

  }
  /** 风格选择 */
  style(id) {
    console.log(id)
    $('.btns').eq(id).addClass('active1')
    $('.rem1').eq(id).show()
  }
  style1(id) {
    console.log(id)
    $('.btns1').eq(id).addClass('active2')
    $('.rem2').eq(id).show()
  }
  style2(id) {
    console.log(id)
    $('.btns2').eq(id).addClass('active3')
    $('.rem3').eq(id).show()
  }
  /** 取消选择 */
  rem1(id) {
    console.log(id)
    $('.rem1').eq(id).hide()
    $('.btns').eq(id).removeClass('active1')
    event.stopPropagation();
  }
  rem2(id) {
    console.log(id)
    $('.rem2').eq(id).hide()
    $('.btns1').eq(id).removeClass('active2')
    event.stopPropagation();
  }
  rem3(id) {
    console.log(id)
    $('.rem3').eq(id).hide()
    $('.btns2').eq(id).removeClass('active3')
    event.stopPropagation();
  }
  /** 重置 */
  all() {
    $('.btns').removeClass('active1')
    $('.btns1').removeClass('active2')
    $('.btns2').removeClass('active3')
    $('.rem1').hide()
    $('.rem2').hide()
    $('.rem3').hide()
  }
  /** 获取款式风格 */
  async getSearchConditionAPI() {
    let res = await this.styleProvider.getSearchConditionAPI(this.data);
    console.log(res);
    this.hands =res.hands;
    this.types =res.type;
    this.tools =res.tools;
    console.log(this.hands);
    console.log(this.types);
    console.log(this.tools);
  }
}


